package com.yxy.config;

import com.yxy.handler.WebSocketServerHandler;
import com.yxy.properties.JwtProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

@Configuration
@EnableWebSocket
@Component
public class WebSocketConfiguration implements WebSocketConfigurer {

    @Autowired
    private JwtProperties jwtProperties;

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        // 直接 new WebSocketServerHandler()，Spring 不会自动注入依赖。
        // 应通过 Spring 容器获取实例，或者使用 @Component 并注入到其他组件中
        registry.addHandler(new WebSocketServerHandler(jwtProperties), "/ws")
                .setAllowedOrigins("*"); // 允许跨域
    }
}
